package com.mhy.netsafe.utils.reg;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Process;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @项目名 Storekeeper
 * @所在包 com.hcsoft.storekeeper.Reg
 * @作者 mahongyin
 * @时间 2019/2/21 10:14
 * @邮箱 mhy.work@qq.com
 * @描述 说明:TODO 添加 激活按钮  后台 验证激活 给做激活操作
 */

public class Reg {
    static boolean zf=false;
    public static String decode(String strEncode) {
        try {
            return new String(Base64.decode(strEncode.getBytes(StandardCharsets.UTF_8), 0), StandardCharsets.UTF_8);//UTf-8
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void onCreate(Context ctx, String url, String key, String app, String info, String qq, String color) {
        Context context = ctx;
        String str = qq;
        String idd = getid(ctx);
        if (idd.equals("")) {
            System.exit(0);
            Process.killProcess(Process.myPid());
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(decode("aHR0cDovLw=="));//http://
        stringBuilder.append(url);
        stringBuilder.append("?key=");
        stringBuilder.append(key);
        stringBuilder.append("&app=");
        stringBuilder.append(app);
        stringBuilder.append("&user=");
        stringBuilder.append(idd);
        send(stringBuilder.toString());
        String zc =callbk; //get(stringBuilder.toString());
        InputStream inputStream;
        LayoutParams txd3;
        if (!"yes".equals(zc)) {
            if (!"no".equals(zc)) {
                if ("dq".equals(zc)) {//已到期
                    toast(context, decode("5bey5Yiw5pyf"));
                } else {//网络连接失败，请打开网络重新验证
                    toast(context, decode("572R57uc6L+e5o6l5aSx6LSl77yM6K+35omT5byA572R57uc6YeN5paw6aqM6K+B"));
                }
            }
            AlertDialog builder = new Builder(context).create();
            LinearLayout lay = new LinearLayout(context);
            lay.setOrientation(LinearLayout.VERTICAL);//1
            LayoutParams ts = new LayoutParams(-1, -2);
            Button tsd = new Button(context);
            tsd.setLayoutParams(ts);
            tsd.setText(info);
            tsd.setTextSize(15.0f);
            tsd.setBackgroundColor(Color.parseColor("#00000000"));
            tsd.setTextColor(Color.parseColor(color));
            tsd.setTop(50);
            lay.addView(tsd, ts);
            InputStream inputStream2 = null;
            try {
                inputStream = ctx.getAssets().open("lh.png");//背景图
            } catch (IOException e) {
                inputStream = inputStream2;
            }
            if (inputStream != null) {
                lay.setBackgroundDrawable(Drawable.createFromStream(inputStream, ""));
            }
            LayoutParams txd23 = new LayoutParams(-1, -2);
            Button text23 = new Button(context);
         //   new Date().getTime();  //获取哦时间
            text23.setLayoutParams(txd23);
            text23.setText(decode("6aqM6K+B"));//验证
            text23.setTextSize(15.0f);
            text23.setTextColor(Color.parseColor(color));
            text23.setBackgroundColor(Color.parseColor("#00000000"));
            String str2 = idd;
            Button text232 = text23;
            String finalIdd = str2;
            LinearLayout lay2 = lay;
            AlertDialog builder2 = builder;
            text232.setOnClickListener(new View.OnClickListener() {
                                           @Override
                                           public void onClick(View view) {
                                               StringBuilder stringBuilder = new StringBuilder();
                                               stringBuilder.append(decode("aHR0cDovLw=="));
                                               stringBuilder.append(url);
                                               stringBuilder.append("?key=");
                                               stringBuilder.append(key);
                                               stringBuilder.append("&app=");
                                               stringBuilder.append(app);
                                               stringBuilder.append("&user=");
                                               stringBuilder.append(idd);
                                               String zc =callbk; //get(stringBuilder.toString());
                                               send(stringBuilder.toString());
                                               if ("yes".equals(zc)) {//恭喜，验证成功
                                                   toast(context, decode("5oGt5Zac77yM6aqM6K+B5oiQ5Yqf"));
                                                   builder.dismiss();
                                               } else if ("no".equals(zc)) { //返回no  请联系我们激活
                                                  toast(context, decode("6K+36IGU57O75oiR5Lus5r+A5rS7"));
                                               } else if ("dq".equals(zc)) {//返回dq  已到期，请联系我们重新激活
                                                   toast(context, decode("5bey5Yiw5pyf77yM6K+36IGU57O75oiR5Lus6YeN5paw5r+A5rS7"));
                                               } else {//网络连接失败，请打开网络重新验证
                                                   toast(context, decode("572R57uc6L+e5o6l5aSx6LSl77yM6K+35omT5byA572R57uc6YeN5paw6aqM6K+B"));
                                               }
                                           }

                                       }
                   // 1 (url, key, app, str2, ctx, builder)
            );


            lay2.addView(text232, txd23);
            LayoutParams txd2 = new LayoutParams(-1, -2);
            Button text2 = new Button(context);
            text2.setLayoutParams(txd2);
            text2.setText(decode("5aSN5Yi2"));//复制
            text2.setTextSize(15.0f);
            text2.setTextColor(Color.parseColor(color));
            text2.setBackgroundColor(Color.parseColor("#00000000"));
            text2.setOnClickListener(new View.OnClickListener() {
                                         @Override
                                         public void onClick(View view) {
                                             if (zf) {
                                                 // TODO: 2019/2/22 待做: 支付
//                                                intent
                                                 text2.setText(decode("5aSN5Yi2"));//复制
                                                 zf=false;
                                             } else {
                                                 ((ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE)).setPrimaryClip(ClipData.newPlainText("Label", finalIdd));
                                                 StringBuilder stringBuilder = new StringBuilder();
                                                 stringBuilder.append(decode("5bey5aSN5Yi2"));
                                                 stringBuilder.append(finalIdd);
                                                 toast(context, stringBuilder.toString());
                                                 text2.setText(decode("5r+A5rS7"));//激活
                                                 zf=true;
                                             }
                                         }
                                     }
                    );
            lay2.addView(text2, txd2);
            if (!str.equals("")) {
                txd3 = new LayoutParams(-1, -2);
                text23 = new Button(context);
                text23.setLayoutParams(txd3);
                text23.setText(decode("5Yqg576k"));//加群    、、5YWz5rOo 关注
                text23.setTextSize(15.0f);
                text23.setTextColor(Color.parseColor(color));
                text23.setBackgroundColor(Color.parseColor("#00000000"));
                text23.setOnClickListener(new View.OnClickListener() {
                                              @Override
                                              public void onClick(View v) {
                                                  joinQQGroup(context, str);
                                              }
                                          }
                );
                lay2.addView(text23, txd3);
            }
            txd3 = new LayoutParams(-1, -2);
            text23 = new Button(context);
            text23.setLayoutParams(txd2);
            text23.setText(decode("6YCA5Ye6"));//退出
            text23.setTextSize(15.0f);
            text23.setTextColor(Color.parseColor(color));
            text23.setBackgroundColor(Color.parseColor("#00000000"));
            text23.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   System.exit(0);
                    Process.killProcess(Process.myPid());
                }
            });
            lay2.addView(text23, txd3);
            AlertDialog builder3 = builder2;
            builder3.setView(lay2);
            builder3.setCancelable(false);
            builder3.getWindow().setGravity(80);
            builder3.show();
        }
    }//5r+A5rS7  激活

    public static String getid(Context ctx) {
        TelephonyManager tm =(TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);//"phone"
        return Secure.getString(ctx.getContentResolver(), "android_id");
    }
    static String callbk="no";
    private static void send(String url) {
        OkHttpUtils.get().url(url).build().execute(new StringCallback()
        {

            @Override
            public void onError(okhttp3.Call call, Exception e, int id) {
                Log.e("mhy", e + "---------" + e);
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("mhy连接", "返回:" + response);//获取登录结果json

                if ("[失败]系统bug".equals(response)||"no".equals(response)){
                    callbk="no";
                }else{ if ("yes".equals(response)){
                    callbk="yes";
                }else {
                    if ("dq".equals(response)){
                        callbk="dq";
                    }
                    callbk=response;
                }
                }
                    // sendok=true;
                //  SendSmsBean bean = new Gson().fromJson(String.valueOf(response), SendSmsBean.class);//
                //ali解析数据
                // processData(response);
            }
        });

    }
    public static String get(String url) {

        FutureTask<String> task = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                send(url);
                throw new UnsupportedOperationException("Method not decompiled: cn.sg89.zc.lh$5.call():java.lang.String");
              //  return null;
            }
        });// FutureTask( new 5 (url, new StringBuilder()))
        new Thread(task).start();
        try {
            return (String) task.get();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean joinQQGroup(Context ctx, String key) {
        Intent intent = new Intent();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D");
        stringBuilder.append(key);
        intent.setData(Uri.parse(stringBuilder.toString()));
        try {
            ctx.startActivity(intent);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void toast(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();//0
    }
}